<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>重置密码</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Microsoft YaHei', sans-serif;
        }

        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background: #f5f5f5;
        }

        .reset-container {
            width: 400px;
            background: white;
            border-radius: 8px;
            padding: 30px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .close-button {
            float: right;
            font-size: 20px;
            color: #999;
            cursor: pointer;
            border: none;
            background: none;
            padding: 5px;
        }

        h2 {
            color: #00a7ff;
            margin-bottom: 30px;
            text-align: center;
        }

        .input-group {
            margin-bottom: 20px;
            position: relative;
        }

        .input-group input {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
        }

        .phone-prefix {
            position: absolute;
            left: 10px;
            top: 50%;
            transform: translateY(-50%);
            color: #666;
        }

        .phone-input {
            padding-left: 50px !important;
        }

        .code-button {
            position: absolute;
            right: 10px;
            top: 50%;
            transform: translateY(-50%);
            padding: 6px 12px;
            background: #00a7ff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        .code-button:disabled {
            background-color: #ccc !important;
            cursor: not-allowed;
        }

        .submit-button {
            width: 100%;
            padding: 12px;
            background: #00a7ff;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            margin-top: 20px;
        }

        .submit-button:hover {
            background: #0095ee;
        }

        .back-link {
            text-align: center;
            margin-top: 20px;
        }

        .back-link a {
            color: #00a7ff;
            text-decoration: none;
            font-size: 14px;
        }

        .password-rules {
            font-size: 12px;
            color: #666;
            margin-top: 5px;
            padding-left: 12px;
        }
    </style>
</head>
<body>
    <div class="reset-container">
        <button class="close-button" onclick="window.location.href='index.html'">×</button>
        <h2>重置密码</h2>
        
        <div class="input-group">
            <span class="phone-prefix">+86</span>
            <input type="text" class="phone-input" placeholder="请输入手机号" id="phone" />
        </div>
        
        <div class="input-group">
            <input type="text" placeholder="请输入验证码" id="code" />
            <button class="code-button" onclick="sendCode()">获取验证码</button>
        </div>
        
        <div class="input-group">
            <input type="password" placeholder="请输入新密码" id="newPassword" />
            <div class="password-rules">密码必须包含字母和数字，且长度至少10位</div>
        </div>
        
        <div class="input-group">
            <input type="password" placeholder="请确认新密码" id="confirmPassword" />
        </div>
        
        <button class="submit-button" onclick="resetPassword()">重置密码</button>
        
        <div class="back-link">
            <a href="index.html">返回登录</a>
        </div>
    </div>

    <script>
        let countdown = 0;
        let countdownTimer = null;

        function sendCode() {
            const phone = document.getElementById('phone').value;
            const codeButton = document.querySelector('.code-button');
            
            if (countdown > 0) return;
            
            if (!phone) {
                alert('请输入手机号');
                return;
            }
            
            if (!/^1[3-9]\d{9}$/.test(phone)) {
                alert('请输入正确的手机号');
                return;
            }

            fetch('sendCode', {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body: 'phone=' + phone
            }).then(response => response.json())
              .then(data => {
                  if (data.success) {
                      countdown = 60;
                      codeButton.disabled = true;
                      
                      countdownTimer = setInterval(() => {
                          countdown--;
                          codeButton.innerText = `重新发送(${countdown}s)`;
                          
                          if (countdown <= 0) {
                              clearInterval(countdownTimer);
                              codeButton.disabled = false;
                              codeButton.innerText = '获取验证码';
                          }
                      }, 1000);
                      
                      alert("验证码已发送！");
                  } else {
                      alert(data.message || "验证码发送失败！");
                  }
              }).catch(error => {
                  console.error('Error:', error);
                  alert("请求失败，请检查网络连接");
              });
        }

        function validatePassword(password) {
            if (!password) return '密码不能为空';
            if (password.length < 10) return '密码长度必须至少10位';
            if (!/[A-Za-z]/.test(password)) return '密码必须包含字母';
            if (!/[0-9]/.test(password)) return '密码必须包含数字';
            return null;
        }

        function resetPassword() {
            const phone = document.getElementById('phone').value;
            const code = document.getElementById('code').value;
            const newPassword = document.getElementById('newPassword').value;
            const confirmPassword = document.getElementById('confirmPassword').value;

            if (!phone || !code || !newPassword || !confirmPassword) {
                alert('请填写所有字段');
                return;
            }

            const passwordError = validatePassword(newPassword);
            if (passwordError) {
                alert(passwordError);
                return;
            }

            if (newPassword !== confirmPassword) {
                alert('两次输入的密码不一致');
                return;
            }

            fetch('resetPassword', {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body: `phone=${phone}&code=${code}&newPassword=${newPassword}`
            }).then(response => response.json())
              .then(data => {
                  alert(data.message);
                  if (data.success) {
                      window.location.href = 'index.html';
                  }
              }).catch(error => {
                  console.error('Error:', error);
                  alert("请求失败，请检查网络连接");
              });
        }
    </script>
</body>
</html>